﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATMCommLib
{
    public static class CommUtils
    {
        public static byte[] ContentTLVLength2Bytes(byte[] content)
        {
            byte[] LenInfo = new byte[2];

            LenInfo[0] = 0;
            LenInfo[1] = 0;

            if (content.Length >= 65536)
            {
                throw new ArgumentOutOfRangeException("Maximum length is 65535");
            }

            if (content == null || content.Length < 256)
            {
                LenInfo[0] = (byte)(content.Length / 256);
                LenInfo[1] = 0;
            }
            else
            {
                LenInfo[0] = (byte)(content.Length / 256);
                LenInfo[1] = (byte)(content.Length % 256);
            }

            return LenInfo;
        }

        public static byte[] GetBytes(string str)
        {
            return Encoding.UTF8.GetBytes(str);
        }
    }
}
